package com.example.mykotlinapp

import org.jetbrains.anko.collections.forEachReversedByIndex
import java.lang.IllegalArgumentException


/**
 *
 * kotlin第一天 枚举和when
 * @author zhudeshuai
 * @since 2019/6/26
 */

//申明枚举
enum class Color(val r: Int, val g: Int, val b: Int) {
    RED(255, 0, 0), GREEN(0, 255, 0), BLUE(0, 0, 255);

    fun rgb() = (r * 256 + g) * 256 + b
}

//when

private fun getCNColor(color: Color) =
    when (color) {
        Color.RED -> "红色"
        else -> "不是红色"
    }

interface Expr
class Num(val value: Int) : Expr
class Sum(val left: Expr, val right: Expr) : Expr

private fun evl(e: Expr): Int {
    //智能转换
    if (e is Sum) {
        return evl(e.left) + evl(e.right)
    }
    if (e is Num) {
        return e.value
    }
    throw IllegalArgumentException("Unknown expression")
}

//使用if表达式体语法
private fun evl2(e: Expr): Int =
    if (e is Sum) {
        evl(e.left) + evl(e.right)
    } else if (e is Num) {
        e.value
    } else {
        throw IllegalArgumentException("Unknown expression")
    }

//使用when
private fun evl3(e: Expr): Int =
    when (e) {
        is Sum -> evl(e.left) + evl(e.right)
        is Num -> e.value
        else -> throw IllegalArgumentException("Unknown expression")
    }

//使用when加入日志
private fun evl4(e: Expr): Int =
    when (e) {
        is Sum -> {
            val left = evl4(e.left)
            val right = evl4(e.right)
            println("sum:($left,$right)")
            left + right
        }
        is Num -> {
            println(e.value)
            e.value
        }
        else -> {
            throw IllegalArgumentException("Unknown expression")
        }
    }

//while循环和for循环
private fun fizzBuzz(i: Int) =
    when {
        i % 15 == 0 -> "FizzBuzz"
        i % 3 == 0 -> "Fizz"
        i % 5 == 0 -> "Buzz"
        else -> "$i"
    }


private fun main() {
    /*for (i in 1..100) {
        println(fizzBuzz(i))
    }*/
    for (i in 100 downTo 1 step 2){
        println(fizzBuzz(i))
    }
    val list = listOf(1,2,3,4)
    list.forEachReversedByIndex {

    }
    val list2 = arrayListOf(12,3)

    val list3 = ArrayList<Int>()
    list3.forEachReversedByIndex {

    }

}